package com.snowj.apaas.web;

import com.snowj.apaas.domain.auto.Trademark;
import com.snowj.apaas.service.TrademarkService;
import com.snowj.apaas.service.utils.PaginationUtil;
import com.snowj.apaas.service.utils.ResponseUtil;
import com.snowj.apaas.web.vm.DataVm;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Optional;

@Api(value = "商标管理", tags = {"商标管理接口"})
@RestController
@RequestMapping("/api")
public class TrademarkController {

    private final TrademarkService trademarkService;

    public TrademarkController(TrademarkService trademarkService) {
        this.trademarkService = trademarkService;
    }

    /**
     * 统计一年内即将到期的商标
     * @return
     */
    @ApiOperation("统计一年内即将到期的商标")
    @GetMapping("/trademarks/due")
    public ResponseEntity<DataVm<Long>> countBecomeDueTrademark() {
        long total = trademarkService.countBecomeDueTrademark();
        return ResponseEntity.ok(DataVm.of(total));
    }

    /**
     * 查询包括：
     * 业务员，日期（大于小于），合同编号，客户名称，商标名称，商标类别，商标号
     *
     * @return
     */
    @ApiOperation("获取商标列表")
    @GetMapping("/trademarks")
    public ResponseEntity<List<Trademark>> listTrademarks(String salesman, String startDate, String endDate, String contractNumber,
                                                          String clientName, String trademarkName, String trademarkType,
                                                          String trademarkNumber, Pageable pageable) {
        Page<Trademark> page = trademarkService.listTrademarks(salesman, startDate, endDate, contractNumber, clientName, trademarkName, trademarkType, trademarkNumber, pageable);
        HttpHeaders httpHeaders = PaginationUtil.generatePaginationHttpHeaders(page);
        return ResponseEntity.status(HttpStatus.OK)
                .headers(httpHeaders)
                .body(page.getContent());
    }

    /**
     * 根据编号查询商标
     *
     * @param id 商标编号
     * @return
     */
    @ApiOperation("根据编号获取商标")
    @GetMapping("/trademarks/{id}")
    public ResponseEntity<Trademark> findTrademark(@PathVariable Long id) {
        Optional<Trademark> trademarkOptional = trademarkService.findTrademark(id);
        return ResponseUtil.wrapOrNotFound(trademarkOptional);
    }

    /**
     * 创建商标
     *
     * @param trademark
     * @return
     */
    @ApiOperation("创建商标")
    @PostMapping("/trademarks")
    public ResponseEntity<Trademark> createTrademark(@RequestBody Trademark trademark) {
        Trademark trad = trademarkService.createTrademark(trademark);
        return ResponseEntity.ok(trad);
    }

    /**
     * 更新商标
     *
     * @param trademark
     * @return
     */
    @ApiOperation("更新商标")
    @PutMapping("/trademarks")
    public ResponseEntity<Trademark> updateTrademark(@RequestBody Trademark trademark) {
        Trademark trad = trademarkService.updateTrademark(trademark);
        return ResponseEntity.ok(trad);
    }

    /**
     * 删除商标
     * @param id 商标编号
     */
    @ApiOperation("删除商标")
    @DeleteMapping("/trademarks/{id}")
    public void deleteTradeMark(@PathVariable Long id) {
        trademarkService.deleteTradeMark(id);
    }
}
